﻿using UnityEngine;
using System.Collections;
using Global.Tool;

namespace Global.Manager
{
    /// <summary>
    /// 全局游戏管理
    /// </summary>
    public class GameGlobalManager : UnityGolbalSingleton<GameGlobalManager>
    {
        #region 常量
        /// <summary>
        /// 帧率限制
        /// </summary>
        private const int LimitFps = 60;
        #endregion
        #region 帧率变量
        /// <summary>
        /// 帧率更新间隔(单位秒)
        /// </summary>
        public float FUpdateInterval = 0.5f;
        /// <summary>
        /// 最后更新帧率间隔的时间(单位秒)
        /// </summary>
        private float _fLastInterval;
        /// <summary>
        /// 自增帧率变量
        /// </summary>
        private int _iFrames = 0;
        /// <summary>
        /// 游戏帧率
        /// </summary>
        private float _fFps;
        /// <summary>
        /// 模型顶点点数
        /// </summary>
        public static int Verts;
        /// <summary>
        /// 模型面数
        /// </summary>
        public static int Tris;
        /// <summary>
        /// 首次激活
        /// </summary>
        #endregion
        #region Unity事件 (Ctrl+Shlft+M 掉出事件函数表，需要装Unity Fo VS 的插件)
        public override void Awake()
        {
            base.Awake();
            Application.targetFrameRate = LimitFps;

        }
        /// <summary>
        /// 第一次开始更新
        /// </summary>
        public void Start()
        {
            //load all manager 
            InputManager.Instance.Init();
            _fLastInterval = Time.realtimeSinceStartup;
            _iFrames = 0;
        }
        /// <summary>
        /// GUI事件
        /// </summary>
        public void OnGUI()
        {
            GUI.skin.label.normal.textColor = new Color(1, 1, 0, 1);
            var fpsDisplay = "FPS:" + (_fFps).ToString("F2");
            var vertsDisplay = Verts.ToString("#,##0 Verts");
            var trisDisplay = Tris.ToString("#,##0 Tris");
            GUI.Label(new Rect(0, 0, 0, 0), "0000");
            GUILayout.Label(fpsDisplay);
            GUILayout.Label(vertsDisplay);
            GUILayout.Label(trisDisplay);
        }
        /// <summary>
        /// 更新
        /// </summary>
        public void Update()
        {
            ++_iFrames;
            if (!(Time.realtimeSinceStartup > _fLastInterval + FUpdateInterval)) return;
            _fFps = _iFrames / (Time.realtimeSinceStartup - _fLastInterval);
            _iFrames = 0;
            _fLastInterval = Time.realtimeSinceStartup;
            GetObjectStats();
        }
        #endregion
        #region 私有工具方法
        /// <summary>
        /// 获得所有游戏对象的状态
        /// </summary>
        private void GetObjectStats()
        {
            Verts = 0;
            Tris = 0;
            var ob = FindObjectsOfType<GameObject>();
            foreach (var obj in ob)
            {
                GetObjectStats(obj);
            }
        }
        /// <summary>
        /// 获得指定游戏对象状态
        /// </summary>
        /// <param name="obj"></param>
        private void GetObjectStats(GameObject obj)
        {
            var filters = obj.GetComponentsInChildren<MeshFilter>();
            foreach (var f in filters)
            {
                Tris += f.sharedMesh.triangles.Length / 3;
                Verts += f.sharedMesh.vertexCount;
            }
        }
        #endregion
    }
}